package com.miyake.client.datamanage;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Panel;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;

public class ShowEditableTable {
	private DataServiceAsync service = DataService.Util.getInstance();
	
	public ShowEditableTable(final NodeProperty node, Panel panel) {
		final CellTable<String[]> table = new CellTable<String[]>();
		panel.add(table);
		table.setWidth("100%");
		abstract class MyTextColumn<T, C> extends Column<T, C> {
			protected int column;
			public MyTextColumn(Cell cell, int col) {
				super(cell);
				column = col;
			}
		}
		service.getTable(node.getKey(), new AsyncCallback<Table>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Table result) {
				String[] title = result.getTextData().get(0);
				result.getTextData().remove(0);
				for (int i = 0; i < result.getTextData().get(0).length; i++) {
					MyTextColumn<String[], String> nameColumn = new MyTextColumn<String[], String>(new EditTextCell(), i){
						@Override
						public String getValue(String[] object) {
							return object[column];
						}
					};   
					table.addColumn(nameColumn, title[i]);
				}	
				table.setRowData(result.getTextData());
			}
		});

	}
}
